package UIComponents.Messages
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import Classes.GameUtils.GU_btnToMC;

	/**
	 * 滚动条 
	 * @author 邓坚
	 * 
	 */	
	public class ScrollBar extends Sprite
	{
		public var _scrollObject:Sprite;           //滚动块
		public var _scrollFloor:Sprite;            //滚动底板
		public var _percentage:uint;				//当前滚动块在滚动条中的百分比
		public var _downMark:Boolean=false;		//是否按下鼠标
		public var _offsetY:uint=0;				//按下时，鼠标相对滚动块的偏移量
		public var _maxNum:Number;
		public function ScrollBar()
		{
			init();
		}
		
		public function init():void
		{
			_scrollObject = this.getChildByName("scrollObject") as Sprite;
			_scrollFloor  = this.getChildByName("scrollFloor") as Sprite;
			
			GU_btnToMC.addListener(_scrollObject as MovieClip);
			_scrollObject.buttonMode = true;
			addListener();
		}
		
		public function addListener():void
		{
			_scrollObject.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
			addEventListener(MouseEvent.MOUSE_DOWN,thisMouseDownHandler);
		}

		
		public function thisMouseDownHandler(e:Event):void
		{
			var moffsety:uint = _scrollObject.height/2;
			var tp:Number = (mouseY+moffsety) > _scrollFloor.height ? _scrollFloor.height-_scrollObject.height : mouseY-moffsety;
			tp = tp-moffsety<0?0:tp;
			_scrollObject.y = tp;
			
			var p:uint = _scrollObject.y / (_scrollFloor.height-_scrollObject.height) * 100;
			if(this.percentage!=p)
			{
				percentage = p;
				(this.parent as ListMessageItems).reShow(p);
			}
		}
		
		/**
		 * 点击了滚动块
		 * @param e
		 * 
		 */
	    public function mouseDownHandler(e:Event):void
		{
			_downMark = true;
			_offsetY = mouseY-_scrollObject.y;
			this.stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
			this.stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
			e.stopPropagation();
		}
		
		/**
		 * 在影片上移动鼠标 
		 * @param e
		 * 
		 */
		public function mouseMoveHandler(e:Event):void
		{
			
			if(_downMark){
				var newy:Number = mouseY-_offsetY;
				newy = newy<0?0:newy;
				newy = (newy+_scrollObject.height)>_scrollFloor.height?_scrollFloor.height-_scrollObject.height:newy;
				_scrollObject.y = newy;
				
				var p:uint = newy / (_scrollFloor.height-_scrollObject.height) * 100;
				if(p!=percentage)
				{
					percentage = p;
					(this.parent as ListMessageItems).reShow(p);
				}
			}
		}
		
		/**
		 * 在影片上松开鼠标 
		 * @param e
		 * 
		 */
		public function mouseUpHandler(e:Event):void
		{
			_downMark = false;
			this.stage.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
			this.stage.removeEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
		}
		
		
		/**
		 * 百分比 set
		 * @param value
		 * 
		 */
		public function set percentage(value:uint):void
		{
			_percentage = value;
            _scrollObject.y = percentage/100 * (_scrollFloor.height-_scrollObject.height);
		}
		
		/**
		 * 百分比 get 
		 * @return 
		 * 
		 */
		public function get percentage():uint
		{
			return _percentage;
		}
		

		/**
		 * 跟据数量改变滚动块长度 
		 * @param value
		 * 
		 */
		public function set maxNum(value:Number):void
		{
			var itemHeight:uint = 5;
			var minheight:uint = 56;
			//this._scrollObject.visible = false;
			_maxNum = value;
			
			value-=4;
			if(value<=0){
				value=0;
			    //this._scrollObject.visible = false;
			}
			else{
				this._scrollObject.visible = true;
			}

			var th:Number = _scrollFloor.height- itemHeight*value;
			th = th<minheight? minheight : th;
			_scrollObject.height = th;
			
			percentage = _percentage;
		}
		
		public function get maxNum():Number
		{
			return this._maxNum;
		}
	}
}